package com.peanut.service;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.peanut.domain.bo.TabNavigationBarBO;
import com.peanut.domain.po.TabNavigationBarPO;
import com.peanut.service.TabNavigationBarService;
import com.peanut.mapper.TabNavigationBarMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author 张涛
* @description 针对表【tab_navigation_bar】的数据库操作Service实现
* @createDate 2022-05-24 17:03:01
*/
@Service
public class TabNavigationBarServiceImpl extends ServiceImpl<TabNavigationBarMapper, TabNavigationBarPO>
    implements TabNavigationBarService{

    @Resource
    private TabNavigationBarMapper navigationBarMapper;

    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public int addNavigationBar(TabNavigationBarBO bo) {
        if (redisTemplate.hasKey("NavigationBar")){
            redisTemplate.delete("NavigationBar");
            System.out.println("删除成功");
        }
        TabNavigationBarPO navigationBar = new TabNavigationBarPO();
        BeanUtils.copyProperties(bo,navigationBar);
        return navigationBarMapper.insert(navigationBar);
    }

    @Override
    public List<TabNavigationBarPO> findAll() {
        if (redisTemplate.hasKey("NavigationBar")) {
            List navigationBar = (List) redisTemplate.boundValueOps("NavigationBar").get();
            return navigationBar;
        }else {
            List<TabNavigationBarPO> list = navigationBarMapper.selectList(null);
            redisTemplate.boundValueOps("NavigationBar").set(list);
            return list;
        }
    }

    @Override
    public int updateNavigationBar(TabNavigationBarBO bo) {
        if (redisTemplate.hasKey("NavigationBar")){
            redisTemplate.delete("NavigationBar");
            System.out.println("删除成功");
        }
        TabNavigationBarPO navigationBar = new TabNavigationBarPO();
        BeanUtils.copyProperties(bo,navigationBar);
        return navigationBarMapper.updateById(navigationBar);
    }

    @Override
    public int deleteNavigationBar(Long navigationBarId) {
        if (redisTemplate.hasKey("NavigationBar")){
            redisTemplate.delete("NavigationBar");
            System.out.println("删除成功");
        }
        return navigationBarMapper.deleteById(navigationBarId);
    }

}




